//
//  AlertTableViewCell.m
//  gdemm
//
//  Created by 乔明超 on 16/9/26.
//  Copyright © 2016年 Goldencis. All rights reserved.
//

#import "AlertTableViewCell.h"
#import "Masonry.h"
#import "Utils.h"
@interface AlertTableViewCell()
@property (strong, nonatomic) UIImageView *selectedImgView;
@property (strong, nonatomic) UILabel *titleLable;
@property (strong, nonatomic) UILabel *timeLable;
@property (strong, nonatomic) UIView *readIcon;
@end
@implementation AlertTableViewCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupTableViewCell];
    }
    
    return self;
}

- (void)setMessageInfo:(MessageInfo *)messageInfo{
    _messageInfo = messageInfo;
    //获取消息通知标题
    _titleLable.text = messageInfo.noticeTitle;
    _timeLable.text = [self getTime:messageInfo.noticeTime];
    _detailLable.text = messageInfo.noticeCon;
    _detailLable.numberOfLines = 0;
    if (messageInfo.isRead) {
        _readIcon.hidden = YES;
    }else{
        _readIcon.hidden = NO;
    }
    if (messageInfo.selState) {
        if (!messageInfo.selected) {
            _selectedImgView.image = [UIImage imageNamed:@"other_icon_notsel"];
        }else{
        
            _selectedImgView.image = [Utils imageWithKey:@"other_icon_sel"];
        }
        
        if ([DeviceTypeJudge isPadDeviceType]) {
            [_selectedImgView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(20);
                make.centerY.mas_equalTo(self);
                make.width.height.mas_equalTo(48);
            }];
            [_readIcon mas_updateConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(25);
                make.width.height.mas_equalTo(10);
                make.left.mas_equalTo(_selectedImgView.mas_right).offset(14);
                
            }];
        }else{
            [_selectedImgView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(10);
                make.centerY.mas_equalTo(self);
                make.width.height.mas_equalTo(35);
            }];
            [_readIcon mas_updateConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(12);
                make.width.height.mas_equalTo(10);
                make.left.mas_equalTo(_selectedImgView.mas_right).offset(10);
                
            }];
        }
        
    }else{
        
        if ([DeviceTypeJudge isPadDeviceType]) {
            [_selectedImgView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(20);
                make.centerY.mas_equalTo(self);
                make.width.height.mas_equalTo(0);
            }];
            messageInfo.selected = NO;
            [_readIcon mas_updateConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(25);
                make.width.height.mas_equalTo(10);
                make.left.mas_equalTo(_selectedImgView.mas_right).offset(0);
                
            }];
        }else{
            [_selectedImgView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(8);
                make.centerY.mas_equalTo(self);
                make.width.height.mas_equalTo(0);
            }];
            messageInfo.selected = NO;
            [_readIcon mas_updateConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(12);
                make.width.height.mas_equalTo(10);
                make.left.mas_equalTo(_selectedImgView.mas_right).offset(0);
                
            }];
        }
        
    }
}
- (void)setupTableViewCell{
    self.backgroundColor = [UIColor clearColor];
    self.selectionStyle = UITableViewCellSelectionStyleNone;

    //添加文件类型imageView
    _selectedImgView = [[UIImageView alloc]init];
    [self.contentView addSubview:_selectedImgView];
    
    //未读图标View
    _readIcon = [[UIView alloc]init];
    _readIcon.backgroundColor = [Utils UIColorFromHex:0XFACF4D];
    _readIcon.layer.cornerRadius = 5;
    [self.contentView addSubview:_readIcon];
    
    //标题label
    _titleLable = [[UILabel alloc]init];
    _titleLable.textColor = [Utils UIColorFromHex:0Xecfaff];
    _titleLable.font = [UIFont systemFontOfSize:18];
    [self.contentView addSubview:_titleLable];
    
    //详细细节label
    _detailLable = [[UILabel alloc]init];
    _detailLable.textColor = [Utils UIColorFromHex:0X8db0de];
    _detailLable.font = [UIFont systemFontOfSize:14];
    _detailLable.numberOfLines = 1;
    _detailLable.lineBreakMode  = NSLineBreakByTruncatingTail;
    [self.contentView addSubview:_detailLable];
    
    //时间label
    _timeLable = [[UILabel alloc]init];
    _timeLable.textAlignment = NSTextAlignmentRight;
    _timeLable.textColor = [Utils UIColorFromHex:0Xffffff];
    _timeLable.font = [UIFont systemFontOfSize:16];
    [self.contentView addSubview:_timeLable];
    
    //线条view
    self.lineView = [[UIView alloc]init];
    [self.contentView addSubview:self.lineView];
    
    if ([DeviceTypeJudge isPadDeviceType]) {
        [self setupIPadTableViewCell];
    }else{
        
        [self setupIPhoneTableViewCell];
    }
    
}
- (void)setupIPadTableViewCell{
    //文件类型imageView约束
    [_selectedImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.centerY.mas_equalTo(self);
        make.width.height.mas_equalTo(0);
    }];
    
    //未读图标imageView约束
    [_readIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(25);
        make.width.height.mas_equalTo(10);
        make.left.mas_equalTo(_selectedImgView.mas_right).offset(0);
        
    }];
    
    //标题titleLabel约束
    [_titleLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.mas_top).offset(22);
        make.left.mas_equalTo(_readIcon.mas_right).offset(10);
    }];
    
    
    //时间timeLabel约束
    [_timeLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(_readIcon.centerY);
        make.right.mas_equalTo(-16);
        
    }];
    
    //详细细节detailLabel约束
    [_detailLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_titleLable.mas_bottom);
        make.left.mas_equalTo(_titleLable.mas_left);
        make.right.mas_equalTo(self.mas_right).offset(-100);
        make.bottom.mas_equalTo(self.mas_bottom);
    }];
    
    //线条line约束
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_titleLable.mas_left);
        make.right.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
        make.height.mas_equalTo(1);
    }];
}
- (void)setupIPhoneTableViewCell{
    //文件类型imageView约束
    [_selectedImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(8);
        make.centerY.mas_equalTo(self);
        make.width.height.mas_equalTo(0);
    }];
    
    //未读图标imageView约束
    [_readIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(17);
        make.width.height.mas_equalTo(10);
        make.left.mas_equalTo(_selectedImgView.mas_right).offset(10);
        
    }];
    
    //标题titleLabel约束
    _titleLable.font = [UIFont systemFontOfSize:14];
    [_titleLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(_readIcon.mas_centerY);
        make.left.mas_equalTo(_readIcon.mas_right).offset(6);
        make.right.mas_equalTo(_timeLable.mas_left).offset(-10);
    }];
    
    
    //时间timeLabel约束
    _timeLable.font = [UIFont systemFontOfSize:11];
    [_timeLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(_readIcon.centerY);
        make.right.mas_equalTo(-16);
        make.width.mas_equalTo(50);
    }];
    
    //详细细节detailLabel约束
    _detailLable.font = [UIFont systemFontOfSize:11];
    [_detailLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_titleLable.mas_bottom);
        make.left.mas_equalTo(_titleLable.mas_left);
        make.right.mas_equalTo(_timeLable.mas_left);
        make.bottom.mas_equalTo(self.mas_bottom).offset(-2);
    }];
    
    //线条line约束
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_titleLable.mas_left);
        make.right.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
        make.height.mas_equalTo(1);
    }];
}
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
-(NSString * )getTime:(NSString *) date{
    NSDateFormatter * dateFormat = [[NSDateFormatter alloc]init];
    [dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    [dateFormat setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
    NSDate *showDate =[dateFormat dateFromString:date];
    NSCalendar * calendar = [NSCalendar currentCalendar];
    NSDateComponents * components =  [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:showDate toDate:[NSDate date] options:NSCalendarWrapComponents];
    if (components.year) {
        [dateFormat setDateFormat:@"MM-dd HH:mm"];
    }else{
        if (components.month) {
            [dateFormat setDateFormat:@"MM-dd HH:mm"];
        }else{
            [dateFormat setDateFormat:@"HH:mm"];
        }
    }

    
    return [dateFormat stringFromDate:showDate];
    
    
}

@end
